package com.fate.system.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fate.framework.dto.IdParamDTO;
import com.fate.framework.exception.KKException;
import com.fate.framework.utils.CommonUtil;
import com.fate.framework.utils.SystemUtil;
import com.fate.framework.vo.LabelValueVO;
import com.fate.system.dto.dict.DictAddDTO;
import com.fate.system.dto.dict.DictPageDTO;
import com.fate.system.dto.dict.DictUpdateDTO;
import com.fate.system.entity.Dict;
import com.fate.system.service.IDictService;
import com.fate.system.vo.dict.DictDetailVO;
import com.fate.system.vo.dict.DictListVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.web.bind.annotation.*;

@Tag(name = "字典操作 API")
@RestController
@RequestMapping("/api/v1.0/dict")
public class DictController {

  @Resource private IDictService dictService;

  @SaCheckRole("super_admin")
  @Operation(summary = "新增")
  @PostMapping("/add")
  public void add(@RequestBody @Valid DictAddDTO addDTO) {
    Dict dict = SystemUtil.buildResource(addDTO, Dict.class);
    dictService.save(dict);
  }

  @SaCheckRole("super_admin")
  @Operation(summary = "删除")
  @PostMapping("/remove")
  public void remove(@RequestBody @Valid IdParamDTO idParamDTO) {
    dictService.removeById(idParamDTO.getId());
  }

  @SaCheckRole("super_admin")
  @Operation(summary = "更新")
  @PostMapping("/update")
  public void update(@RequestBody @Valid DictUpdateDTO updateDTO) {
    Dict dict = SystemUtil.buildResource(updateDTO, Dict.class);
    dictService.updateById(dict);
  }

  @SaCheckRole("super_admin")
  @Operation(summary = "详情")
  @PostMapping("/detail")
  public DictDetailVO detail(@RequestBody @Valid IdParamDTO idParamDTO) {
    Dict dict = dictService.getById(idParamDTO.getId());
    return SystemUtil.buildResource(dict, DictDetailVO.class);
  }

  @Operation(summary = "获取下拉列表")
  @GetMapping("/getSelects")
  public List<LabelValueVO> getSelects(String type) {
    List<Dict> dictList = dictService.getDictByType(type);
    if (CommonUtil.isNullOrEmpty(dictList)) {
      throw new KKException("字典类型不存在");
    }

    return dictList.stream()
        .map(d -> new LabelValueVO(d.getDescription(), d.getValue()))
        .collect(Collectors.toList());
  }

  @Operation(summary = "获取字典分页列表")
  @PostMapping("/getDictList")
  public Page<DictListVO> getDictList(@RequestBody @Valid DictPageDTO pageDTO) {
    String type = pageDTO.getType();
    String moduleName = pageDTO.getModuleName();

    Page<Dict> page = SystemUtil.buildPageFromDTO(pageDTO, Dict.class);
    page =
        dictService
            .lambdaQuery()
            .like(CommonUtil.notNullOrEmpty(type), Dict::getType, type)
            .like(CommonUtil.notNullOrEmpty(moduleName), Dict::getModuleName, moduleName)
            .orderByDesc(Dict::getType)
            .page(page);

    return SystemUtil.buildResource(page, DictListVO.class);
  }

  @Operation(summary = "获取字典模块列表")
  @PostMapping("/getDictModuleList")
  public List<LabelValueVO> getDictModuleList(){
    List<Dict> dictList = dictService.lambdaQuery().list();
    if (CommonUtil.isNullOrEmpty(dictList)) {
      return new ArrayList<>();
    }
    return dictList.stream()
        .map(d -> new LabelValueVO(d.getModuleName(), d.getType()))
        .distinct()
        .collect(Collectors.toList());
  }
}
